package cn.senjoeson.healthsport.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.senjoeson.healthsport.GreenDaoManager;
import cn.senjoeson.healthsport.R;
import cn.senjoeson.healthsport.bean.User;
import cn.senjoeson.healthsport.dao.UserDao;

/**
 * @author Android
 * @date 2018/1/2
 */

public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {


    @BindView(R.id.et_id)
    EditText mEtId;
    @BindView(R.id.et_password)
    EditText mEtPassword;
    @BindView(R.id.et_confirmpwd)
    EditText mEtConfirmpwd;
    @BindView(R.id.realname)
    EditText mRealname;
    @BindView(R.id.male)
    CheckBox mMale;
    @BindView(R.id.female)
    CheckBox mFemale;
    @BindView(R.id.btn_register)
    Button mBtnRegister;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);
        initView();
        initListener();
    }

    private void initListener() {
        mMale.setOnClickListener(this);
        mFemale.setOnClickListener(this);
        mBtnRegister.setOnClickListener(this);
    }

    public void checkSelected(boolean isMan) {
        mMale.setChecked(isMan);
        mFemale.setChecked(!isMan);
    }


    private void initView() {


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.male:
                checkSelected(true);
                break;
            case R.id.female:
                checkSelected(false);
                break;
            case R.id.btn_register:
                register();
                break;
        }
    }

    private void register() {
        final UserDao userDao = GreenDaoManager.getInstance().getNewSession().getUserDao();
        String username = mEtId.getText().toString().trim();
        String password = mEtPassword.getText().toString().trim();
        String comfirmpasword = mEtConfirmpwd.getText().toString().trim();
        String realname = mRealname.getText().toString().trim();
        // String gender = mGender.getText().toString().trim();
        if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password) || TextUtils.isEmpty(comfirmpasword)) {
            Toast.makeText(RegisterActivity.this, "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return;
        } else if (!TextUtils.equals(password, comfirmpasword)) {
            Toast.makeText(RegisterActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        user.setGender(mMale.isChecked() ? "男" : "女");
        user.setRealname(realname);
        userDao.insertWithoutSettingPk(user);
        finish();
    }
}


